import { Component, Input, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { EventService } from 'src/app/shared/service/event.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';

@Component({
  selector: 'app-second-kill-list',
  templateUrl: './second-kill-list.component.html',
  styleUrls: ['./second-kill-list.component.scss'],
})
export class SecondKillListComponent implements OnInit {
  @Input() tagData: any;
  showLoadMore = false;
  pageNo = 1;
  pageSize: number;
  pageTotal: number;
  params: any = { content: {} };


  constructor( 
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private httpService: HttpService,
    private alertController: AlertController,
    private eventService: EventService
    ) { }

  ngOnInit(

  ) {
    this.pageTotal = this.tagData.pageTotal;
    this.pageNo = this.tagData.pageNo;
    this.pageSize = this.tagData.pageSize;
    this.params.content = this.tagData.data.submit;
    this.params.sessionId = localStorage.getItem("sessionId");
    // tslint:disable-next-line:max-line-length
    this.showLoadMore = !this.tagData.data.moreButton && this.tagData.data.submit && this.pageTotal && (this.pageNo < Math.ceil(this.pageTotal / this.pageSize));
    this.getPercentage(this.tagData.data.data);
  }
  itemClick(item) {

    // 要先执行一个beforeSubmit，返回true再执行link
    if (item.beforeSubmit &&  item.beforeSubmit.submit) {
      const params = {
        content: item.beforeSubmit.submit.content,
        id: item.beforeSubmit.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      this.httpService.post("application/submit", params, res => {
        if (res.success) {
          this.pageModalService.openPage(item.link);

        } else {
          this.appGlobalService.showToast(res.message, 1500, 'middle');
        }
      });
    } else {
      this.pageModalService.openPage(item.link);

    }

  }
  loadMoreData(event) {
    try {
      if (!this.params) {
        this.params = { content: {} };
        this.params = this.tagData.data.submit;
      }
      this.pageNo = this.pageNo + 1;// // 参数
      this.params.content.pageNo = this.pageNo;
      this.params.id = this.tagData.data.submit.id;
      this.params.sessionId = localStorage.getItem("sessionId");
      // 请求获取新数据
      this.httpService.post('application/submit', this.params, res => {

        // 请求数据成功
        if (res.success) {
          if (res.data.submit) {
            this.params = res.data.submit; // 为了每次刷新下一页的参数 "oldIndex"
          }
          const resList = res.data.data;
          // 追加返回数据
          if (resList && resList.length > 0) {
            // 追加数据
            this.getPercentage(resList);
            resList.map(v => {
              this.tagData.data.data.push(v);
            });
          } else {

            event.target.disabled = true;
          }
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          // this.appGlobalService.showToast(res.message, 2000, 'middle');
        }
        event.target.complete();

      }, err => {
        // 请求失败，更新当前页数
        this.pageNo = this.pageNo - 1;
        // 请求数据失败
        // this.appGlobalService.showToast(err.message, 2000, 'middle');
        event.target.complete();

      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }
  }

  parseFloatStr(item) {
    return parseFloat(item);
  }

  getPercentage(item) {
    if (item && item[0] && item[0].firstRowLeft && item[0].firstRowLeft.firstText.label !== undefined) {
      item.forEach(e => {
        e.firstRowLeft.firstText.label = this.parseFloatStr(e.firstRowLeft.firstText.label);
        if (e.firstRowLeft.firstText.label < 13) {
          e.firstRowLeft.firstText.cssStyle += "width:13%;" + "background:transparent;";
        } else {
          e.firstRowLeft.firstText.cssStyle += "width:" + e.firstRowLeft.firstText.label + "%;";
        }
      });
    }
  }

  // 抢购按钮
  itemClick2(link, e?) {
    if (e) {
      e.stopPropagation();
    }
    this.pageModalService.openPage(link);
  }
}
